import { createRouter, createWebHistory } from 'vue-router'
import { useStore } from '../stores/userStore'
import ManageHome from '../views/manage/ManageHome.vue'
import ManageClubs from '../views/manage/ManageClubs.vue'
import ManageSchools from '../views/manage/ManageSchools.vue'
import ManageUsers from '../views/manage/ManageUsers.vue'
import ManagePosts from '../views/manage/ManagePosts.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Find',
      component: () => import('../views/Find.vue'),
    },
    {
      path: '/MyClub',
      name: 'MyClub',
      component: () => import('../views/MyClub.vue'),
    },
    {
      path: '/club/:id',
      name: 'Club',
      component: () => import('../views/Club.vue'),
    },
    {
      path: '/club/:id/manage',
      name: 'ClubManage',
      component: () => import('../views/club/ClubManage.vue'),
      meta: { requiresClubAdmin: true },
      children: [
        {
          path: 'info',
          name: 'ClubInfoEdit',
          component: () => import('../views/club/ClubInfoEdit.vue')
        },
        {
          path: 'members',
          name: 'ClubMembers',
          component: () => import('../views/club/ClubMembers.vue')
        },
        {
          path: 'activities',
          name: 'ClubActivities',
          component: () => import('../views/club/ClubActivities.vue')
        },
        {
          path: 'applications',
          name: 'ClubApplications',
          component: () => import('../views/club/ClubApplications.vue')
        },
        {
          path: 'posts',
          name: 'ClubPosts',
          component: () => import('../views/club/ClubPosts.vue')
        },
        {
          path: '',
          redirect: to => ({ name: 'ClubInfoEdit', params: { id: to.params.id } })
        }
      ]
    },
    // 学校管理路由
    {
      path: '/school/:id/manage',
      name: 'SchoolManage',
      component: () => import('../views/school/SchoolManage.vue'),
      meta: { requiresSchoolAdmin: true },
      children: [
        {
          path: 'info',
          name: 'SchoolInfoEdit',
          component: () => import('../views/school/SchoolInfoEdit.vue')
        },
        {
          path: 'clubs',
          name: 'SchoolClubs',
          component: () => import('../views/school/SchoolClubs.vue')
        },
        {
          path: 'applications',
          name: 'SchoolApplications',
          component: () => import('../views/school/SchoolApplications.vue')
        },
        {
          path: 'members',
          name: 'SchoolMembers',
          component: () => import('../views/school/SchoolMembers.vue')
        },
        {
          path: 'posts',
          name: 'SchoolPosts',
          component: () => import('../views/school/SchoolPosts.vue')
        },
        {
          path: '',
          redirect: to => ({ name: 'SchoolInfoEdit', params: { id: to.params.id } })
        }
      ]
    },
    {
      path: '/Self',
      name: 'Self',
      component: () => import('../views/Self.vue'),
    },
    {
      path: '/MyU',
      name: 'MyU',
      component: () => import('../views/MyU.vue'),
    },
    {
      path: '/publish',
      name: 'Publish',
      component: () => import('../views/Publish.vue'),
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../views/Register.vue'),
    },
    {
      path: '/ceshi',
      name: 'ceshi',
      component: () => import('../views/ceshi.vue'),
    },
    {
      path: '/chooseSchool',
      name: 'chooseSchool',
      component: () => import('../views/chooseSchool.vue'),
    },
    // 管理员子系统路由
    {
      path: '/manage',
      name: 'Manage',
      component: ManageHome,
      meta: { requiresAdmin: true },
      children: [
        {
          path: 'clubs',
          name: 'ManageClubs',
          component: ManageClubs
        },
        {
          path: 'schools',
          name: 'ManageSchools',
          component: ManageSchools
        },
        {
          path: 'users',
          name: 'ManageUsers',
          component: ManageUsers
        },
        {
          path: 'posts',
          name: 'ManagePosts',
          component: ManagePosts
        },
        {
          path: '',
          redirect: { name: 'ManageClubs' }
        }
      ]
    },
  ],
})

// 路由守卫 - 检查权限
// router.beforeEach((to, from, next) => {
//   const store = useStore();

//   // 检查系统管理员权限
//   if (to.matched.some(record => record.meta.requiresAdmin)) {
//     if (!store.hasAdminRole()) {
//       next('/login');
//     } else {
//       next();
//     }
//   }
//   // 检查社团管理员权限
//   else if (to.matched.some(record => record.meta.requiresClubAdmin)) {
//     if (!store.user.loggedIn) {
//       next('/login');
//     } else {
//       // 检查是否有clubAdmin角色
//       if (store.user.roles.includes('clubAdmin')) {
//         // 实际应用中，还需要检查是否是特定社团的管理员
//         // 这需要从后端获取数据验证
//         next();
//       } else {
//         // 不是社团管理员，重定向到社团页面
//         next(`/club/${to.params.id}`);
//       }
//     }
//   } else {
//     next();
//   }
// });

export default router
